package com.furnitur.modules.portal.controller;

import com.furnitur.common.api.Result;
import com.furnitur.core.entity.address.Address;
import com.furnitur.modules.portal.dto.address.AddressDTO;
import com.furnitur.modules.portal.service.PortalAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/portal/address")
@Validated
public class PortalAddressController {

    @Autowired
    private PortalAddressService addressService;

    @PostMapping
    public Result<Address> addAddress(@Valid @RequestBody AddressDTO addressDTO) {
        return Result.success(addressService.addAddress(addressDTO));
    }

    @PutMapping("/{id}")
    public Result<Address> updateAddress(
            @PathVariable Long id,
            @Valid @RequestBody AddressDTO addressDTO) {
        return Result.success(addressService.updateAddress(id, addressDTO));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteAddress(@PathVariable Long id) {
        addressService.deleteAddress(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Address>> getAddressList() {
        return Result.success(addressService.getAddressList());
    }

    @GetMapping("/default")
    public Result<Address> getDefaultAddress() {
        return Result.success(addressService.getDefaultAddress());
    }

    @PutMapping("/{id}/default")
    public Result<Void> setDefaultAddress(@PathVariable Long id) {
        addressService.setDefaultAddress(id);
        return Result.success();
    }
} 